#include "gl_supported.h"

int gl_extension_supported(const char *extension)
{
    static const GLubyte *extensions = NULL;
    const GLubyte *start;
    GLubyte *where, *terminator;

    // Extension names should not have spaces.
    where = (GLubyte *) std::strchr(extension, ' ');

    if (where || *extension == '\0')
        return 0;

    if (!extensions)
        extensions = glGetString(GL_EXTENSIONS);

    if (extensions == NULL)
        _err("gl_extension_supported()", "extensions missing or GL hasn't been initialised");

    // It takes a bit of care to be fool-proof about parsing the
    // OpenGL extensions string.  Don't be fooled by sub-strings,
    // etc.
    start = extensions;

    for (;;) {
        where = (GLubyte *) std::strstr((const char *) start, extension);

        if (!where)
            break;

        terminator = where + std::strlen(extension);

        if (where == start || *(where - 1) == ' ') {
            if (*terminator == ' ' || *terminator == '\0') {
                return 1;
            }
        }

        start = terminator;
    }

    return 0;
}
